package com.lianzhihui.romeex.ui.mianfragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.CoinAdapter;
import com.lianzhihui.romeex.bean.WalletMainBean;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.mvp.presenter.WalletInfoPresenter;
import com.lianzhihui.romeex.mvp.view.WalletInfoView;
import com.lianzhihui.romeex.ui.account.WalletManagerActivity;
import com.lianzhihui.romeex.ui.base.BaseFragment;
import com.lianzhihui.romeex.ui.wallet.BaizhantingActivity;
import com.lianzhihui.romeex.ui.wallet.CoinActivityType1;
import com.lianzhihui.romeex.ui.wallet.CoinActivityType4;
import com.lianzhihui.romeex.ui.wallet.CoinActivityType2;
import com.lianzhihui.romeex.ui.wallet.CoinActivityType3;
import com.lianzhihui.romeex.ui.wallet.ShouqianActivity;
import com.lianzhihui.romeex.ui.wallet.ZhuanZhangActivity;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.NumberUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.utils.UserUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.lianzhihui.romeex.utils.UserUtils.getCurrentUserMoney;

/**
 * Description: 钱包首页
 * Author: RAMON
 * CreateDate: 2020-11-2 14：00
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class WalletFragment extends BaseFragment implements WalletInfoView, OnRefreshListener {
    // the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    @BindView(R.id.tv_address)
    TextView addressTv;
    @BindView(R.id.refresher)
    SmartRefreshLayout refresher;
    @BindView(R.id.iv_eyes)
    ImageView iv_eyes;

    @BindView(R.id.tv_assets)
    TextView tv_assets;
    @BindView(R.id.tv_abort)
    TextView tv_abort;
    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.bt_zhuanzhang)
    Button bt_zhuanzhang;
    @BindView(R.id.bt_shouqian)
    Button bt_shouqian;
    @BindView(R.id.bt_baizhanting)
    Button bt_baizhanting;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    WalletInfo walletInfo;

    @BindView(R.id.status_bar_id)
    View statusBarId;

    CoinAdapter adapter;

    WalletInfoPresenter presenter;
    WalletMainBean bean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    public static WalletFragment newInstance() {
        WalletFragment fragment = new WalletFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, "");
        args.putString(ARG_PARAM2, "");
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wallet;
    }

    @Override
    protected void initView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarId.getHeight());
        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(MyApplication.mContext);
        statusBarId.setLayoutParams(layoutParams);


        refresher.setOnRefreshListener(this);

        iv_eyes.setSelected(UserUtils.getCanSee());

        presenter = new WalletInfoPresenter(activity, this);
        presenter.getWalletInfo();
        walletInfo = UserUtils.getCurrentUser();

        if (null == walletInfo) {
            //暂不处理
        } else {
            LogUtil.error("ramon", walletInfo.toString());
            addressTv.setText(walletInfo.getUserName());
            initRecyclerView();
        }

    }

    private void initRecyclerView() {
        if (adapter == null) {
            adapter = new CoinAdapter(new ArrayList<>());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
            adapter.setEmptyView(getEmptyView());
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    WalletMainBean.CoinBean bean = (WalletMainBean.CoinBean) adapter.getItem(position);
                    Intent intent = new Intent();
                    if (bean == null) {
                        return;
                    }

                    switch (bean.getType()) {
                        case 1:
                            intent.setClass(activity, CoinActivityType1.class);
                            break;
                        case 2:
                            intent.setClass(activity, CoinActivityType2.class);
                            break;
                        case 3:
                            intent.setClass(activity, CoinActivityType3.class);
                            break;
                        case 4:
                            intent.setClass(activity, CoinActivityType4.class);
                            break;
                        default:
                            break;
                    }

                    intent.putExtra("coin", (WalletMainBean.CoinBean) adapter.getItem(position));
                    startActivity(intent);

                }
            });
        } else {
            adapter.setNewData(bean.getList());
        }
    }

    @OnClick({R.id.tv_address, R.id.bt_zhuanzhang, R.id.bt_shouqian, R.id.iv_eyes, R.id.bt_baizhanting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_address:
                //钱包管理
                startActivity(new Intent(activity, WalletManagerActivity.class));
                break;
            case R.id.bt_zhuanzhang:
                //转账
                startActivity(new Intent(activity, ZhuanZhangActivity.class).putExtra("tradeType", "1").putExtra("isall", true));
                break;
            case R.id.bt_shouqian:
                //收款
                startActivity(new Intent(activity, ShouqianActivity.class).putExtra("tradeType", "2").putExtra("isall", true));
                break;
            case R.id.bt_baizhanting:
                //拜占庭
                startActivity(new Intent(activity, BaizhantingActivity.class));
//                toast(getResources().getString(R.string.text_will_be_open));
                break;
            case R.id.iv_eyes:
                if (iv_eyes.isSelected()) {
                    //设置不可见
                    iv_eyes.setSelected(false);
                } else {
                    iv_eyes.setSelected(true);
                }
                UserUtils.saveCanSee(iv_eyes.isSelected());
                ctrlSee();
                break;
            default:
                break;
        }
    }

    @Override
    public void onGetWalletInfo(WalletMainBean bean) {
        //load data
        this.bean = bean;
        loadData();
        refresher.finishRefresh();
    }

    @Override
    public void onGetWalletInfoFail(Object obj) {
        refresher.finishRefresh();
    }

    @Override
    public void setFailure() {

    }

    @SuppressLint("SetTextI18n")
    private void loadData() {
        LogUtil.error("ramon","加载资产数据");
        ctrlSee();
        initRecyclerView();
        AppConstants.System.coins = bean.getList();
    }

    private void ctrlSee() {
        boolean cansee = UserUtils.getCanSee();

        if (cansee) {
            //可见
            tv_assets.setText(NumberUtils.big8(bean.getCommon().getTotalBalance()));
            switch (UserUtils.getCurrentyType()) {
                case 1:
                    tv_abort.setText(NumberUtils.big2(bean.getCommon().getTotalBalanceCNY()));
                    break;
                case 2:
                    tv_abort.setText(NumberUtils.big2(bean.getCommon().getTotalBalanceUSDT()));
                    break;
                default:
                    break;
            }
            tv_type.setText(getCurrentUserMoney());
        } else {
            tv_assets.setText("****");
            tv_abort.setText("****");
            tv_type.setText("");
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        presenter.getWalletInfo();
    }

    @Override
    public void onMessageEvent(MessageEvent event) {
        super.onMessageEvent(event);
        if (event.getCode() == AppConstants.Event.REFRESHER_ASSETS) {
            LogUtil.error("ramon","刷新资产");
            refresher.autoRefresh();
        }
    }
}